package com.seeyon.apps.kkARPEvent.util;

import com.seeyon.ctp.cap.api.manager.CAPFormManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ModuleType;
import com.seeyon.ctp.common.affair.manager.AffairManager;
import com.seeyon.ctp.common.content.comment.Comment;
import com.seeyon.ctp.common.content.comment.CommentManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.i18n.ResourceUtil;
import com.seeyon.ctp.common.permission.bo.Permission;
import com.seeyon.ctp.common.permission.manager.PermissionManager;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.organization.dao.OrgHelper;
import com.seeyon.ctp.workflow.util.MessageUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Title :
 * Author: chenxd
 * Date: 2023-12-4 10:58
 */
public class PDFCommonUtils {

    private static PDFCommonUtils instance;
    public static PDFCommonUtils getInstance(){
        if(instance == null){
            synchronized (PDFCommonUtils.class){
                if(instance == null){
                    instance = new PDFCommonUtils();
                }
            }
        }
        return instance;
    }

    private static Map<String,String> cacheMap = new HashMap<>();
    public String getPath(String affairId){
        return cacheMap.get(affairId);
    }

    public void putPath(String affairId,String path){
        cacheMap.put(affairId,path);
    }

    private static final Log logger = LogFactory.getLog(PDFCommonUtils.class);
    private static AffairManager affairManager = (AffairManager) AppContext.getBean("affairManager");
    private static CAPFormManager capFormManager = (CAPFormManager) AppContext.getBean("capFormManager");
    private static PermissionManager permissionManager = (PermissionManager) AppContext.getBean("permissionManager");
    private static CommentManager ctpCommentManager = (CommentManager) AppContext.getBean("ctpCommentManager");

    public String getFormDataHtmlForRest(Long formAppId, Long moduleId, String rightId) {
        try {
            capFormManager.addRightId(formAppId, rightId);
            String bodyContent = capFormManager.getFormDataHtmlForForward(formAppId, 1,moduleId, rightId);
            logger.info("bodyContent:"+bodyContent);
            return bodyContent;
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return null;
    }
    public String formatFileName(String subject) {
        Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
        Matcher matcher = pattern.matcher(subject);
        subject = matcher.replaceAll("");
        if(subject.length() > 237) {
            subject = subject.substring(0, 237) + "•••";
        }
        return subject;
    }

    public String coverCommentHtml(ModuleType moduleType,Long moduleId,int width) throws BusinessException {
        //获取所有意见
        List<Comment> comments = ctpCommentManager.getCommentAllByModuleId(moduleType, moduleId);
        if(CollectionUtils.isEmpty(comments)){
            return "";
        }
        StringBuffer commentStr = new StringBuffer();
        commentStr.append(" <div class=\"contentDiv\" align=\"center\" style=\"margin-top:10px;\">")
                .append("<table class=\"contentClass\" cellspacing=\"0\" cellpadding=\"0\" id=\"contentClass\" style=\"border-collapse:collapse;height: auto;width:")
                .append(width).append("px;\">")
                .append("<tr style=\"border: 1px solid #f00;\"><td style=\"border: 1px solid #f00;\" width=\"17%\">处理环节</td><td style=\"border: 1px solid #f00;\" width=\"13%\">操作类型</td><td style=\"border: 1px solid #f00;\" width=\"13%\">处理人员</td><td style=\"border: 1px solid #f00;\" width=\"21%\">处理描述</td><td style=\"border: 1px solid #f00;\" width=\"21%\">附件</td><td style=\"border: 1px solid #f00;\" width=\"15%\">完成时间</td></tr>");

        for (Comment comment : comments) {
            CtpAffair ctpAffair = affairManager.get(comment.getAffairId());
            commentStr.append("<tr style=\"border: 1px solid #f00;\">");
            commentStr.append("<td style=\"border: 1px solid #f00;\">").append(ctpAffair.getNodeName()).append("</td>");//处理环节
            commentStr.append("<td style=\"border: 1px solid #f00;\">").append(showPolicyName(ctpAffair.getNodePolicy())).append("</td>");//操作类型
            commentStr.append("<td style=\"border: 1px solid #f00;\">").append(OrgHelper.showMemberName(comment.getCreateId())).append("</td>");//处理人员
            commentStr.append("<td style=\"border: 1px solid #f00;\">").append(comment.getContent()).append("</td>");//处理描述
            commentStr.append("<td style=\"text-align:left;border: 1px solid #f00;\">").append("").append("</td>");//附件(暂时为空)
            commentStr.append("<td style=\"border: 1px solid #f00;\">").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(comment.getCreateDate())).append("</td>");//完成时间
            commentStr.append("</tr>");
        }
        commentStr.append("</table></div>");
        return commentStr.toString();
    }

    private String showPolicyName(String policy){
        String name = MessageUtil.getString("node.policy." + policy);
        if (null != name && !name.equalsIgnoreCase("node.policy." + policy)) {
            return name;
        } else {
            // 取一下自定义的节点权限名称
            Permission permission = permissionManager.getGlobalPermission(policy);
            return permission != null ? permission.getLabel() : ResourceUtil.getDataI18nString(policy);
        }
    }

    public boolean isForm(Long affairId) throws BusinessException {
        CtpAffair ctpAffair = affairManager.get(affairId);
        return ctpAffair.getFormAppId() != null;
    }

}
